from .pub_data import METHODS_INFO
from .handle import Request, RequestHeaders


class Process:
    PREFIX = "m_"

    # 可能要上数据库了

    def __init__(self, protocol: Request):
        """
        方法的定义需要严格的按照顺序，这是约定
        :param protocol:
        """
        self.protocol = protocol
        self.headers = protocol.headers
        self.body: dict = protocol.body
        self.methods = {
            k: v for k, v in
            enumerate(
                filter(
                    lambda item: True if item[0].startswith(self.PREFIX) else False, self.__class__.__dict__.items()),
                1)
        }

    def m_register(self, info: dict, headers: RequestHeaders):
        """
        1
        :return:
        """
        name = info.get("name")  # 用户的昵称
        password = info.get("password")  # 用户密码
        # 先查数据库，看库中是否有同样昵称的人，两个策略支持昵称相同，或者不支持
        # 使用一个随机的数字6为以上，不能全0或者全1，作为用户的账号，
        # 密码应该使用hash算法进行单向加密
        # 将这些信息存储到数据库中
        # 注册成功，直接返回用户的账号

    def m_login(self, info: dict, headers: RequestHeaders):
        """
        2
        :return:
        """
        account = info.get("account")
        password = info.get("password")
        print("-" * 100)
        print("login account {} password {}".format(account, password))
        # 目前用户只支持使用账号进行登录，同时传递密码
        # 登陆成功后在请求头中返回正确的状态码，同时应该给用户算出一个token值，防止用户再次登录

    def m_logout(self, info: dict, headers: RequestHeaders):
        """
        3
        :return:
        """
        account = info.get("account")
        print("-" * 100)
        print("logout", account)
        # 直接让用户的token过期，应该注意，在任何的非登录注册等业务函数中，都应该首先检查用户的token值
        # 确定是合法的，才能执行业务函数，否则必须重新登录成功后才可以

    def m_destruct(self, info: dict, headers: RequestHeaders):
        """
        4
        :return:
        """
        return
        # account = info.get("account")
        # password = info.get("password")
        # print(account)
        # print(password)

    def m_add_friend(self, info: dict, headers: RequestHeaders):
        """
        5
        :return:
        """
        number = info.get("number")
        print("-" * 100)
        # password = info.get("password")
        print("add_friend number {}".format(number))
        # print(password)
        # 用户给定一个账号，用来添加为其好友，应该先检查该账号是否存在，且要注意，好友只能加一遍，不能重复添加

    def m_del_friend(self, info: dict, headers: RequestHeaders):
        """
        6
        :return:
        """
        number = info.get("number")
        print("-" * 100)
        # password = info.get("password")
        print("del_friend number {}".format(number))
        # print(password)
        # 删除一个好友，只能删除已经添加过得好友，而且确保给定的账号是存在的，可以在前端加一些控制

    def m_create_group(self, info: dict, headers: RequestHeaders):
        """
        7
        :return:
        """
        group = info["name"]
        print("-" * 100)
        print(info)
        print("create new group {}".format(group))
        # 创建一个群，应该给定群的昵称，这个功能跟注册账号有点相似
        # 以后可能会有很多相关的功能实现，比如，禁言，管理员，群成员有多少，等等

    def m_add_group(self, info: dict, headers: RequestHeaders):
        """
        8
        :return:
        """
        group = info["number"]
        print("-" * 100)
        print("add group {}".format(group))
        # 跟加人相似

    def m_del_group(self, info: dict, headers: RequestHeaders):
        """
        9
        :return:
        """
        group = info["number"]
        print("-" * 100)
        print("del group {}".format(group))
        # 记不清该业务是干嘛的了，我目前想的是退群，而不是解散群，这个功能貌似容易实现些

    def m_send_img(self, info: dict, headers: RequestHeaders):
        """
        10
        :return:
        """
        pic = info["pic"]
        print("-" * 100)
        print("pic {}".format(pic))
        # 测试功能，可能需要再自己实现个协议吧，表情协议

    def m_show_friends(self, info: dict, headers: RequestHeaders):
        """
        11
        :return:
        """
        print("-" * 100)
        print("u fri")
        # 显示自己的好友信息，貌似比较简单

    def m_show_groups(self, info: dict, headers: RequestHeaders):
        """
        12
        :return:
        """
        print("-" * 100)
        print("u grp")
        # 显示自己都加了哪些群

    def m_search_friend(self, info: dict, headers: RequestHeaders):
        """
        13
        :return:
        """
        friend = info["number"]
        print("-" * 100)
        print("search friend {}".format(friend))
        # 搜索好友，添加好友的子功能

    def m_search_group(self, info: dict, headers: RequestHeaders):
        """
        14
        :return:
        """
        group = info["name"]
        print("-" * 100)
        print(info)

        print("search group {}".format(group))
        # 搜索群，添加群的子功能

    def m_modify_info(self, info: dict, headers: RequestHeaders):
        """
        15
        :return:
        """
        new_info = info["info"]
        print("-" * 100)
        print(info)

        print("info {}".format(new_info))
        # 修改自己的个人信息，其实某些信息应该注册的时候就给定

    def m_modify_password(self, info: dict, headers: RequestHeaders):
        """
        16
        :return:
        """
        password = info["password"]
        print("-" * 100)
        print("password {}".format(password))
        # 修改密码，只能在已登录的状态下才能修改，是以后的找回密码的子功能，比较复杂

    def m_upload_img(self, info: dict, headers: RequestHeaders):
        """
        17
        :return:
        """
        img = info["img"]
        print("-" * 100)
        print(info)

        print("img {}".format(img))
        # 上传头像，目前暂未考虑实现

    def m_send_msg(self, info: dict, headers: RequestHeaders):
        """
        18
        :return:
        """
        msg = info["msg"]
        print("-" * 100)
        print(info)

        print("msg {}".format(msg))
        # 发送消息，最基本的功能，必须事先，指定群号，或者是用户账号进行发送，

    def schedule(self):
        # self.headers.method
        method = self.headers.method
        fn = self.methods.get(method)[1]
        # TODO  终于修改了这个bug  舒服了
        fn(self, {k: getattr(self.body, k) if k else None for k in METHODS_INFO[method]}, self.headers)

    def run(self):
        self.schedule()
